package com.yyk.yiliao.ui.activity.detail.fragment;


import android.os.Bundle;
import androidx.annotation.Nullable;

import android.widget.ScrollView;
import android.widget.TextView;

import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseFragment;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ebs.MessageEvent;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.StoreInfotwo_Info;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ${zk} on 2018/8/14 0014.
 */

public class Detail_Fragment_Two extends BaseFragment {
    public static final String ARGS_PAGE = "Detail_Fragment_Two";


    @BindView(R.id.scrollView)
    ScrollView scrollView;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.name2)
    TextView name2;
    @BindView(R.id.name3)
    TextView name3;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.name4)
    TextView name4;
    @BindView(R.id.name5)
    TextView name5;
    @BindView(R.id.name6)
    TextView name6;
    @BindView(R.id.name7)
    TextView name7;
    @BindView(R.id.name8)
    TextView name8;
    @BindView(R.id.name9)
    TextView name9;
    private int id;

    public static Detail_Fragment_Two newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARGS_PAGE, page);
        Detail_Fragment_Two fragment = new Detail_Fragment_Two();
        fragment.setArguments(args);
        return fragment;
    }

    private void initGetIntent() {
        Bundle arguments = getArguments();
        id = arguments.getInt("id");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        initGetIntent();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMoonEvent(MessageEvent messageEvent) {
        id = Integer.parseInt(messageEvent.getMessage());
    }



    @Override
    public int getLayoutId() {
        return R.layout.fragment_detail_two;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        setView();

    }

    private void setView() {
        Map<String, String> map = new TreeMap<>();
        map.put("id", id + "");
        map.put("sign", Md5Util2.createSign(map));
        initXq(map);

    }

    private void initXq(Map<String, String> map) {
        RxUtils.getInstance().retrofit.create(ApiService.class).postStoreInfoTwo(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<StoreInfotwo_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(StoreInfotwo_Info info) {
                        if (info.getCode() == 1) {
                            StoreInfotwo_Info.DataBean data = info.getData();
                            name.setText(data.getBrand());
                            name2.setText(data.getType());
                            name3.setText(data.getPackageX());
                            name4.setText(data.getDose());
                            name5.setText(data.getCode());
                            name6.setText(data.getUselife());
                            name7.setText(data.getEnterprise());
                            name8.setText(data.getUselife());
                            if (data.getRecipe() == 0) {
                                name9.setText("非处方药");
                            } else {
                                name9.setText("处方药");
                            }
                        }
                    }

                });
        try {
            //因为网络请求在子线程，所以不能让宿主线程过早的结束。
            Thread.sleep(100);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

    }


    /**
     * 动态添加
     */
//    final List<StoreInfotwo_Info.DataBean> list = new ArrayList<>();
//                            list.add(info.getData());
//    BaseRecyclerAdapter<StoreInfotwo_Info.DataBean> adapter = new BaseRecyclerAdapter<StoreInfotwo_Info.DataBean>(getActivity(), list, R.layout.adapter_item_detail_xiangqing) {
//        private String[] name;
//        private String[] title = {"品牌名称", "产品剂量", "套餐类型", "使用剂量", "批准文号", "生产企业", "生产企业", "用法", "处方"};
//
//        @Override
//        public void convert(BaseRecyclerHolder holder, StoreInfotwo_Info.DataBean item, int position, boolean isScrolling) {
//            StoreInfotwo_Info.DataBean dataBean = list.get(position);
//            int recipe = dataBean.getRecipe();
//            if (recipe == 0) {
//                name = new String[]{dataBean.getBrand(), dataBean.getType(), dataBean.getPackageX(), dataBean.getDose(), dataBean.getCode(), dataBean.getUselife(), dataBean.getEnterprise(), dataBean.getUsetype(), "非处方"};
//
//
//            } else {
//                name = new String[]{dataBean.getBrand(), dataBean.getType(), dataBean.getPackageX(), dataBean.getDose(), dataBean.getCode(), dataBean.getUselife(), dataBean.getEnterprise(), dataBean.getUsetype(), "处方"};
//
//            }
//            for (int i = 0; i < name.length; i++) {
//                holder.setText(R.id.title, title[i]);
//                holder.setText(R.id.name, name[i]);
//            }
//
//        }
//    };
//
//                            rv.setLayoutManager(new LinearLayoutManager(getActivity()));
//                            rv.setAdapter(adapter);
}
